//
//  DDTheBackgroundImageController.m
//  DDMusic
//
//  Created by 何星宏 on 2016/11/14.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDTheBackgroundImageController.h"
#import "ImageeCell.h"
#import "UIImage+ZoomSize.h"

@interface DDTheBackgroundImageController ()<UICollectionViewDataSource,UICollectionViewDelegate,
ImageeCellDelegate,
UINavigationControllerDelegate,
UIImagePickerControllerDelegate>

@property(nonatomic,strong)UICollectionView* collectionView;
@property(nonatomic,strong)UIImagePickerController* imagePickerVc;
@property(nonatomic,strong)NSMutableArray* dataArray;
@property(nonatomic,assign)NSInteger selectIndex;

@end
static NSString* idntifier = @"collectionCell";

@implementation DDTheBackgroundImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = [NSMutableArray array];
    
    [self.view addSubview:self.collectionView];//添加视图
    //注册原形cell
    [self.collectionView registerClass:[ImageeCell class] forCellWithReuseIdentifier:idntifier];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DDTheBackgroundImageControllerDismiss" object:self.dataArray];

}

#pragma mark -- 数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArray.count +1;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //从缓存中获得cell，必须先注册cell
    ImageeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:idntifier forIndexPath:indexPath];
    //    cell.backgroundColor = [UIColor redColor];
    //设置cell代理
    cell.delegate = self;
    //传递图片给cell
    if (indexPath.item == _dataArray.count) {
        cell.itemImage = nil;
    }else{
        cell.itemImage = _dataArray[indexPath.item];
    }
    
    
    
    
    return cell;
}

#pragma mark -- 自定义cell的代理方法
- (void)ImageeCellAddImageBtnClickedWithCell:(ImageeCell *)cell{
    //    NSLog(@"添加图片");
    //根据cell获得对应的下标
    _selectIndex = [self.collectionView indexPathForCell:cell].item;
    
    
    //1、弹窗
    UIAlertController* alertVc = [UIAlertController alertControllerWithTitle:@"请选择图片来源" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    //添加相册按钮
    UIAlertAction* photoAlbum = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //打开相册
        [self openphotoAlbum];
    }];
    [alertVc addAction:photoAlbum];
    //添加相机按钮
    UIAlertAction* Camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //打开相机
        [self openCamera];
    }];
    [alertVc addAction:Camera];
    //添加取消按钮
    UIAlertAction *cacelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVc addAction:cacelAction];
    [self presentViewController:alertVc animated:YES completion:nil];
    
}

//打开相册的方法
- (void)openphotoAlbum{
    //1、先判断当前设备是否能够打开相册
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
        //设置数据来源
        self.imagePickerVc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        //打开相册
        [self presentViewController:self.imagePickerVc animated:YES completion:nil];
    }else{
        NSLog(@"打开相册失败");
    }
    
}


//打开相机的方法
- (void)openCamera{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //设置数据来源
        self.imagePickerVc.sourceType = UIImagePickerControllerSourceTypeCamera;
        //打开相机
        [self presentViewController:self.imagePickerVc animated:YES completion:nil];
    }else{
        NSLog(@"打开相机失败");
    }
}

//删除按钮的代理方法
- (void)ImageeCellDeleteImageBtnClickedWithCell:(ImageeCell *)cell{
    NSLog(@"删除图片");
    
    //改变数据源数组
    [_dataArray removeObject:cell.itemImage];
    
    //刷新
    [self.collectionView reloadData];
}

#pragma mark -- 相册的代理方法

//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
//    NSLog(@"点击了取消按钮");
//}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //先退出当前控制器，实现代理方法后需手动设置退出
    [self dismissViewControllerAnimated:self.imagePickerVc completion:nil];
    //如果有编辑模式开启就使用编辑后的图片，否则使用原图
    UIImage * selectImage = nil;
    if (self.imagePickerVc.allowsEditing) {
        selectImage = info[UIImagePickerControllerEditedImage];
    }else{
        
        // 获得图片
        selectImage = info[UIImagePickerControllerOriginalImage];
    }
    //根据当前点击的cell来判断是添加还是修改
    if (_selectIndex == _dataArray.count) {
        //添加图片到数组
        [_dataArray addObject:selectImage];
    }else{
        //覆盖之前的图片
        _dataArray[_selectIndex] = selectImage;
    }
    //压缩图片
    selectImage = [selectImage resizedImagetoWidth:100];
    
    //刷新视图
    [self.collectionView reloadData];
    
    
}

#pragma mark --懒加载

- (UIImagePickerController *)imagePickerVc{
    if (!_imagePickerVc) {
        _imagePickerVc = [[UIImagePickerController alloc]init];
        _imagePickerVc.delegate = self;
        
        _imagePickerVc.allowsEditing = YES;//设置可编辑图片
    }
    return _imagePickerVc;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        //创建布局对象
        UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
        //设置间距
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        flowLayout.itemSize = CGSizeMake(80, 80);
        
        //设置滚动方向
        //        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        //使用布局对象初始化集合视图
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        //设置背景色
        _collectionView.backgroundColor = [UIColor whiteColor];
        //设置代理和数据源
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}








@end
